package com.cty;

import com.cty.ioc_01.XxxDao;
import com.cty.ioc_01.XxxService;
import com.cty.ioc_02.JavaBean;
import com.cty.ioc_03.UserController;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IocTest_01 {
    @Test
    public void test_01() {
        ClassPathXmlApplicationContext applicationContext
                = new ClassPathXmlApplicationContext("spring-01.xml");
        XxxDao dao = applicationContext.getBean(XxxDao.class);
        System.out.println("Dao" + dao);
        XxxService xxxService = applicationContext.getBean("xxxService", XxxService.class);
        System.out.println("Service" + xxxService);
    }


    @Test
    public void test_02() {
        ClassPathXmlApplicationContext applicationContext
                = new ClassPathXmlApplicationContext("spring-02.xml");
        JavaBean bean = applicationContext.getBean(JavaBean.class);
        JavaBean bean1 = applicationContext.getBean("javaBean", JavaBean.class);
        System.out.println(bean1 == bean);
        //多例模式不调用销毁方法
        applicationContext.close();
    }
    @Test
    public void test_03() {
        ClassPathXmlApplicationContext applicationContext
                = new ClassPathXmlApplicationContext("spring-03.xml");
        //有且只有一个对象，直接装配
        UserController userController =
                applicationContext.getBean(UserController.class);

        //当ioc中有两个相同的对象时:就会报错
        /* 解决方法
        * 1.将全局属性改名该成与想要的对象一样的名字
        * 2.第二种方法:将全局属性上面多加一个注解@Qualifier(value = "userServiceImpl")
        * value是你想要对象的名字
        *  优化方法:@Resource(name = "userServiceImpl") = @Qualifier(value = "userServiceImpl") + @Autowired
        *   该注解需要导入一个依赖才能使用
        * */
        userController.show();
        applicationContext.close();
    }
}
